<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | create by allen.qiang
// +----------------------------------------------------------------------


/**
 * 商品秒杀ajax类
 * @package  htdocs
 * @subpackage  quanliadmin
 * @author   create by allen.qiang
 */

quanli_require('module/base/goodsHandler.class.php');
quanli_require('module/base/promotionHandler.class.php');

class ajax_seckill{

    public function execute(){
        switch ($_REQUEST['operFlg']){
            case 1:   //闪购活动查询
                $this->seckillList();
                break;
            case 2:     //保存秒杀活动
                $this->saveSeckill();
                break;
            case 3:     //改变使用状态
                $this->changeStauts();
                break;
            case 4:     //删除秒杀
                $this->delSeckill();
                break;
            default:
                break;
        }

        return;

    }

    /*
     * 秒杀列表信息
     */
    private function seckillList(){

        global $G_PGLOB;

        //促销类
        $promotionHandler = new promotionHandler($G_PGLOB->DBCA);

        //查询
        $result_list = $promotionHandler->searchSeconKillList($_REQUEST);

        
        $preview_url = echo_preview_url();
        
        //返回数组
        $result_array = array(
            'page' => $_REQUEST['page'],
            'total' => $_REQUEST['count'],
            'rows' => array()
        );

        if ($result_list){

            foreach ($result_list as $seckill){

                //使用状态
                if($seckill['useFlg'] == 1){
                    $status = "启用";
                }else{
                    $status = "禁用";
                }

                $result_array['rows'][] = array(
                    'id' => $seckill['kill_id'],
                    'cell' => array(
                        '<a href="javascript:void(0);" onclick="seckill.edit(' . $seckill['kill_id'] . ');">修改</a>&nbsp;&nbsp;'.
                        '<a target="_blank" href="/shop/secondsKill.php?&'.$preview_url.'&subsite=shop">预览</a>',
                        '<a href="javascript:void(0);" onclick="seckill.change.confim(' . $seckill['kill_id'] .', '.$seckill['useFlg'] . ');" >' . $status . '</a>',
                        $seckill['kill_id'],
                        $seckill['product_code'],
                        $seckill['goodsName'],
                        $seckill['startDate'],
                        $seckill['endDate'],
                        $seckill['kill_price'],
                        $seckill['max_sale_amount'],
                        $seckill['saled_amount'],
                        $seckill['praise'],
                    )
                );

            }

        }

        echo json_encode($result_array);

        return;
    }


    /**
     * 保存商品秒杀
     * $_POST['data'] = array (
     *              kill_id => int 秒杀号
     *              postID => int postID
     *              kill_price => decimal
     *              max_sale_amount =>  int  秒杀数量
     *              useFlg => int 使用标志
     * )\
     */
    private function saveSeckill(){

        global $G_PGLOB;

        //促销类
        $promotionHandler = new promotionHandler($G_PGLOB->DBCA);

        $_POST['creator'] = $G_PGLOB->currentUserID;
        $result = $promotionHandler->saveSecondKill( $_POST );

        echo json_encode($result);

    }

    /**
     * 改变使用状态
     */
    private function changeStauts() {

        global $G_PGLOB;

        if(!isset($_REQUEST['id']) || !is_numeric($_REQUEST['id']) ){
            die( json_encode( array( 'code' => 400 , 'message' => '参数错误' ) ) );
        }

        //实例化促销活动类
        $promotionHandler = new promotionHandler( $G_PGLOB->DBCA );

        //修改活动使用状态
        $result_array = $promotionHandler->changeSecondKillUse( $_REQUEST['id'] );

        echo json_encode( $result_array );

    }

    /**
     * 删除秒杀
     */
    private function delSeckill() {

        global $G_PGLOB;
        
        //促销类
        $promotionHandler = new promotionHandler( $G_PGLOB->DBCA );

        $result_array = $promotionHandler->deleteSecondKill( $_POST['kill_array'] );

        echo json_encode($result_array);

    }


}